/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { getAll as getAllVendor } from "$/attractions/inspection/api/inspectionVendors.ts";
import { page as getDeviceTypesPage } from "$/attractions/inspection/api/inspectionDeviceTypes.ts";
import { deleteByIds } from "$/attractions/inspection/api/inspectionDevices.ts";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const msg = useMessage();

  const vendorList = ref<any[]>([]);
  const deviceTypeList = ref<any[]>([]);

  onMounted(async () => {
    // 获取供应商列表
    const vendorResponse = await getAllVendor();
    if (
      vendorResponse.code === 200 &&
      vendorResponse.data &&
      vendorResponse.data.list
    ) {
      vendorList.value = vendorResponse.data.list;
    }

    // 获取设备类型列表
    const deviceTypeResponse = await getDeviceTypesPage({});
    if (
      deviceTypeResponse.code === 200 &&
      deviceTypeResponse.data &&
      deviceTypeResponse.data.list
    ) {
      // console.log("设备类型列表:", deviceTypeResponse.data.list);
      deviceTypeList.value = deviceTypeResponse.data.list;
    }
  });

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    { label: "设备名称", prop: "name" },
    { label: "设备型号", prop: "model" },
    { label: "经度", prop: "longitude" },
    { label: "纬度", prop: "latitude" },
    { label: "海拔高度", prop: "altitude" },
    {
      label: "设备类型",
      prop: "type_id",
      cellRender: ({ row }) => {
        const deviceTypeItem = deviceTypeList.value.find(
          (item) => item.id === row.type_id
        );
        return deviceTypeItem
          ? deviceTypeItem.name
          : `未知设备类型(${row.type_id})`;
      },
    },
    {
      label: "设备状态",
      prop: "status",
      cellRender: ({ row }) => {
        const statusMap = {
          1: { text: "在线", type: "success" },
          2: { text: "离线", type: "danger" },
        };
        const status = statusMap[row.status] || { text: "未知", type: "info" };
        return h(ElTag, { type: status.type }, () => status.text);
      },
    },
    {
      label: "供应商",
      prop: "vendor_id",
      cellRender: ({ row }) => {
        const vendorItem = vendorList.value.find(
          (item) => item.id === row.vendor_id
        );
        return vendorItem ? vendorItem.name : `未知供应商(${row.vendor_id})`;
      },
    },
    {
      label: "服务商",
      prop: "service_id",
      cellRender: ({ row }) => {
        const vendorItem = vendorList.value.find(
          (item) => item.id === row.service_id
        );
        return vendorItem ? vendorItem.name : `未知服务商(${row.service_id})`;
      },
    },
    { label: "关联设备", prop: "device" },
    {
      type: "operation",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) =>
              hasAuth("inspection_devices:inspectionDevices:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) =>
              hasAuth("inspection_devices:inspectionDevices:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
